package pe.org.cnl.congreso.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class CsvUtil {

	public static List<List<String>> csvToList(String fileName){
		try{
			FileReader fir = new FileReader(fileName);
			return csvToList( fir );
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}	
	}
	
	/**
	 * Separado por | (pipe)<br>
	 * convierte eltexto a UTF-8<br>
	 * elimina comillas<br>
	 * elimina -- usados para cargar excel<br>
	 * @param fr filereader
	 * @return
	 */
	public static List<List<String>> csvToList(FileReader fr){
		List<List<String>> cellDataList = new ArrayList<List<String>>();
		 List<String> tempList = null;
		 String token = ""; 
		try{
		      BufferedReader br = new BufferedReader(fr);
		      String stringRead = br.readLine();
		      while( stringRead != null ){
		        StringTokenizer st = new StringTokenizer(stringRead, "|");
		        tempList = new ArrayList<String>();
		        while(st.hasMoreElements()){
		        	token = new String( st.nextToken().replaceAll("\"", "").replaceAll("--", "").getBytes() , "UTF-8"); 
		        	tempList.add( token );
		        	System.out.print( token  );
		        }
		        System.out.println("");
		        cellDataList.add(tempList);
		        stringRead = br.readLine();
		      }
		      br.close( );
		}catch (Exception e){
			e.printStackTrace();
		}
		return cellDataList;
	}
}
